package root.electronclass.electronclass.grade.homework

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet.Layout.UNSET
import androidx.core.view.isVisible
import androidx.core.view.marginTop
import androidx.core.view.setPadding
import androidx.core.view.updateLayoutParams
import root.electronclass.electronclass.R
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.BasicDialogFragment
import root.electronclass.electronclass.databinding.FragmentClassHomeworkDetailDialogBinding
import root.electronclass.electronclass.databinding.FragmentClassHomeworkDetailImageItemBinding
import root.electronclass.electronclass.ext.dp
import root.electronclass.electronclass.ext.setVideoThumbnail
import root.electronclass.electronclass.grade.model.ClassHomeworkItem
import root.electronclass.electronclass.other.PictureActivity
import root.electronclass.electronclass.other.VideoActivity

/**
 *
 * @author Lokiy
 * @date 2023/2/18 21:10
 */
class ClassHomeworkDetailDialog : BasicDialogFragment<FragmentClassHomeworkDetailDialogBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.update(arguments?.getParcelable(DATA) ?: return, true)
        updateDialogWidth(952)
        binding.root.post {
            binding.root.scrollTo(0, 0)
        }
    }

    companion object {
        const val DATA = "_DATA"
    }
}

fun FragmentClassHomeworkDetailDialogBinding.update(item: ClassHomeworkItem, isInDialog: Boolean = false) {
    if (isInDialog) {
        root.setPadding(40)
        name.isVisible = false
        startTimeTitle.updateLayoutParams<ConstraintLayout.LayoutParams> { topMargin = 0 }
        endTimeTitle.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = 0

            topToBottom = UNSET
            topToTop = R.id.start_time_title

            startToStart = UNSET
            startToEnd = R.id.start_time
            marginStart = 204
        }
        noteTitle.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = 0

            topToBottom = UNSET
            topToTop = R.id.elapsed_time_title

            startToStart = R.id.end_time_title
        }
        content.updateLayoutParams<ConstraintLayout.LayoutParams> {
            startToStart = UNSET
            topToBottom = UNSET

            topMargin = 0
            topToTop = R.id.content_title
            startToEnd = R.id.content_title
        }
        imagesTitle.isVisible = false
    }
    val context = root.context
    name.text = item.name
    subject.text = item.subject
    startTime.text = item.startTime
    endTime.text = item.endTime
    elapsedTime.text = if (item.elapsedTime > 0) context.getString(R.string.minute, item.elapsedTime.toInt()) else "-"
    note.text = item.note
    content.text = item.content.orEmpty()
    images.adapter = BasicAdapter<String, FragmentClassHomeworkDetailImageItemBinding> { holder, picItem ->
        holder.binding.itemImage.setImageURI(picItem)
        if (isInDialog) {
            holder.binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                width = 200
                height = 200
                marginEnd = 24
            }
        }
        holder.binding.itemImage.setOnClickListener {
            PictureActivity.startActivity(context, ArrayList(list), holder.adapterPosition)
        }
    }.apply {
        refresh(item.picList?.map { it.url })
    }

    images.isVisible = item.picList?.isNotEmpty() == true
    imagesTitle.isVisible = item.picList?.isNotEmpty() == true

    video.clipToOutline = true
    video.setVideoThumbnail(item.videoList?.getOrNull(0)?.url)
    video.isVisible = item.videoList?.isNotEmpty() == true
    videoTitle.isVisible = item.videoList?.isNotEmpty() == true
    videoPlay.isVisible = item.videoList?.isNotEmpty() == true
    video.setOnClickListener {
        VideoActivity.startActivity(context, item.videoList?.getOrNull(0)?.url ?: return@setOnClickListener)
    }

}
